package com.dianrui.fmi.activity;

import android.app.ProgressDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dianrui.fmi.Constant;
import com.dianrui.fmi.R;
import com.dianrui.fmi.event.ChangeName;
import com.dianrui.fmi.event.RfreshUserImg;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangNameActivity extends BaseActivity {
    private String mName;
    private ProgressDialog mProgressDialog;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.edit_activity_change_info)
    EditText editChangeName;


    @Override
    public int getContentViewId() {
        return R.layout.activity_change_name;
    }

    @Override
    public void initView() {
        mProgressDialog=new ProgressDialog(this);
        if (getIntent() != null && getIntent().getExtras() != null) {
            mName = getIntent().getStringExtra("nickname");
        }
        title.setText(getString(R.string.change_name));
        editChangeName.setText(mName);
        topRight.setText(getString(R.string.save));
        topRight.setVisibility(View.VISIBLE);
    }



    @OnClick({R.id.back, R.id.top_right, R.id.name_update_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.top_right:
                if (StringUtils.isEmpty(editChangeName.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.please_input_names));
                } else {
                    setInfoInfo();
                }
                break;

            case R.id.name_update_img:
                editChangeName.setText("");
                break;

            default:
                break;
        }
    }


    private void setInfoInfo() {
        mProgressDialog.setMessage(getString(R.string.submiting));
        mProgressDialog.show();
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(false);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("nickname", editChangeName.getText().toString().trim());
        OkHttpUtils.getInstance().postWithMemberToken(Url.SETINFO, jsonObject.toString(), new OkHttpUtils.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            mProgressDialog.dismiss();
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                spUtils.put("nickname", jsonObject.optJSONObject("data").optString("nickname"));
                                spUtils.put("member_id", jsonObject.optJSONObject("data").optString("member_id"));
                                EventBus.getDefault().post(new ChangeName(jsonObject.optJSONObject("data").optString("nickname")));
                                EventBus.getDefault().post(new RfreshUserImg());
                                ToastUtil.showToast(jsonObject.optString("message"));
                                finish();
                            } else {
                                mProgressDialog.dismiss();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    mProgressDialog.dismiss();
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mProgressDialog!=null &&mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
    }
}
